//
//  SystemMediaPickerViewController.m
//  SystemMediaPicker
//
//  Created by 畑圭輔 on 10/02/28.
//  Copyright Keisuke Hata 2010. All rights reserved.
//

#import "SystemMediaPickerViewController.h"

@implementation SystemMediaPickerViewController

#pragma mark -
#pragma mark Action

// iPodライブラリを開く
- (IBAction) openiPodLibrary
{
	// ピッカーを生成
	NSLog(@"iPodライブラリを開く");
	MPMediaPickerController *picker = 
	[[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
	
	// デリゲート
	picker.delegate = self;		
	// 複数選択を許可するかどうか？
	picker.allowsPickingMultipleItems = YES;
	// ピッカーのタイトル
	picker.prompt = @"曲追加";
	
	// ピッカーを表示
	[self presentModalViewController:picker animated:YES];
	[picker release];
}

#pragma mark -
#pragma mark MediaPicker Delegate

// 曲を選択した時に呼ばれる
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker 
  didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {

	NSLog(@"曲数:%d" , mediaItemCollection.count );
	
	NSString *songlist = @"";
	int count = 0;
	for (MPMediaItem *song in mediaItemCollection.items) {
		
		NSString *title = [song valueForProperty:MPMediaItemPropertyTitle];
		if( title )
		{
			songlist = [songlist stringByAppendingFormat:@"[%03d]:%@\n" , ++count,title];
		}
	}
	_messageTxt.text = songlist;
	
	// メディアピッカーを隠す時の処理
	[self dismissModalViewControllerAnimated:YES];
}

// 曲をキャンセルした時に呼ばれる
- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
	
	NSLog(@"キャンセル");

	// メディアピッカーを隠す時の処理
	[self dismissModalViewControllerAnimated:YES];
}

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end
